package com.shrw.duke.bluetoothheartrate.ui;

import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.shrw.duke.bluetoothheartrate.R;
import com.shrw.duke.bluetoothheartrate.utils.SPUtils;

public class AddPeopleActivity extends AppCompatActivity implements View.OnClickListener {
    public static final String NAME ="PEOPLE_NAME";
    public static final String ADDRESS="PEOPLE_ADDRESS";
    public static final String FIRST="FIRST";

    private EditText mEditName, mEditAddress;
    private ImageView mImgBtnAdd;
    private TextView mTitle;
    private Button mBtnAdd;

    private SPUtils mSPUtils;
    private String mName;
    private String mAddress;
    private boolean mFirst;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add_people);

        initView();
        initAction();
    }

    @Override
    protected void onStart() {
        super.onStart();
        resetNameAddress();
    }

    private void resetNameAddress() {
        mName = mSPUtils.getString(NAME,"");
        mAddress = mSPUtils.getString(ADDRESS,"");
        mFirst = mSPUtils.getBoolean(FIRST,true);
        mEditName.setText(mName);
        mEditAddress.setText(mAddress);
        if (mFirst){
            mBtnAdd.setText("添加");
        }else {
            mBtnAdd.setText("修改");
        }
    }

    private void initAction() {
        mBtnAdd.setOnClickListener(this);
    }

    private void initView() {
        mEditName = (EditText) findViewById(R.id.people_edit_name);
        mEditAddress = (EditText) findViewById(R.id.people_edit_address);
        mBtnAdd = (Button) findViewById(R.id.people_btn_add);
        mImgBtnAdd = (ImageView) findViewById(R.id.toolbar_menu);
        mTitle = (TextView) findViewById(R.id.toolbar_title);

        mImgBtnAdd.setVisibility(View.GONE);
        mTitle.setText("添加");
        mSPUtils = SPUtils.getInstance(this, "people");

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()){
            case R.id.people_btn_add:
                mName = mEditName.getText().toString();
                mAddress = mEditAddress.getText().toString();
                if (TextUtils.isEmpty(mName)&&TextUtils.isEmpty(mAddress)){
                    Toast.makeText(this, "名字与标签号不能为空", Toast.LENGTH_SHORT).show();
                    break;
                }
                mSPUtils.put(NAME,mName);
                mSPUtils.put(ADDRESS,mAddress);
                if (mFirst){
                    Toast.makeText(this, "添加成功", Toast.LENGTH_SHORT).show();
                }else {
                    Toast.makeText(this, "修改成功", Toast.LENGTH_SHORT).show();
                }
                mSPUtils.put(FIRST,false);
                break;
        }
    }
}
